package com.bosssoft.promotions.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.base.entity.Result;
import com.bosssoft.base.enums.ResultCodeEnum;
import com.bosssoft.promotions.domain.dto.CouponDTO;
import com.bosssoft.promotions.domain.dto.CouponQueryDTO;
import com.bosssoft.promotions.domain.dto.QualifiedCouponQueryDTO;
import com.bosssoft.promotions.domain.po.Coupon;
import com.bosssoft.promotions.domain.vo.CouponVO;
import com.bosssoft.promotions.service.CouponService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /*
     * 用例图完
     */
    @ApiOperation(value = "添加优惠券")
    @PostMapping("add")
    public Result addCoupon(@RequestBody CouponDTO couponDTO){
        if (couponService.addCoupon(couponDTO)) {
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "添加成功", null);
        }
        return Result.build(ResultCodeEnum.ERROR.getCode(), "添加失败", null);
    }

    /*
     * 更新优惠券
     */
    /*
    * 用例图完
    */
    @ApiOperation(value = "更新优惠券")
    @PostMapping("update")
    public Result updateCoupon(@RequestBody CouponDTO couponDTO){
        if (couponService.updateCoupon(couponDTO)) {
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "更新成功", null);
        }
        return Result.build(ResultCodeEnum.ERROR.getCode(), "更新失败", null);
    }

    /*
     * 删除优惠券
     * <只做删除标记，不做实际删除 >
     */
    /*
     * 用例图完
     */
    @ApiOperation(value = "删除优惠券")
    @PostMapping("delete")
    public Result deleteCouponById(@RequestBody CouponQueryDTO couponQueryDTO){
        if (couponService.deleteCouponById(couponQueryDTO)) {
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "删除成功", null);
        }
        return Result.build(ResultCodeEnum.ERROR.getCode(), "删除失败", null);
    }

    /*
     * 批量删除优惠券
     */
    /*
     * 用例图完
     */
    @ApiOperation(value = "批量删除优惠券")
    @PostMapping("batchDelete")
    public Result batchDeleteCouponById(@RequestBody List<CouponQueryDTO> idList){
        if (couponService.batchDeleteCouponById(idList)) {
            return Result.build(ResultCodeEnum.SUCCESS.getCode(), "批量删除成功", null);
        }
        return Result.build(ResultCodeEnum.ERROR.getCode(), "批量删除失败", null);
    }

    /*
     * 查询优惠券
     */
    /*
     * 用例图完
     */
    @ApiOperation(value = "查询优惠券")
    @GetMapping("query")
    public Result queryCoupon(@RequestBody CouponQueryDTO couponQueryDTO){
        Coupon coupon = couponService.queryCoupon(couponQueryDTO);
        CouponVO couponVO = new CouponVO();
        couponVO.setCouponName(coupon.getCouponName());
        couponVO.setType(coupon.getType());
        couponVO.setThreshold(coupon.getThreshold());
        couponVO.setDiscount_price(coupon.getDiscountPrice());
        couponVO.setMount(coupon.getMount());
        couponVO.setBeginTime(coupon.getBeginTime());
        couponVO.setEndTime(coupon.getEndTime());
        couponVO.setStatus(coupon.getStatus());
        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "查询成功", couponVO);
    }

    /*
     * 增加用户优惠券
     */
    /*
    *
    */
    @ApiOperation(value = "增加用户优惠券")
    @GetMapping("addUser")
    public Result add(@RequestBody QualifiedCouponQueryDTO qualifiedCouponQueryDTO){
        couponService.add(qualifiedCouponQueryDTO);
        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "优惠券领取成功", null);
    }

    /*
     * 减少用户优惠券
     */
    /*
     * POSTMAN 测试成功
     */
    @ApiOperation(value = "减少用户优惠券")
    @PostMapping("reduce")
    public Result reduce(@RequestBody QualifiedCouponQueryDTO qualifiedCouponQueryDTO){
        couponService.reduce(qualifiedCouponQueryDTO);
        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "优惠券扣除成功", null);
    }

    /*
     * 查询全部优惠券（分页）
     * 分页没效果
     */
    @ApiOperation(value = "查询全部优惠券 <后台>")
    @GetMapping("queryAllPageCoupon")
    public Result queryAllPageCoupon(int page,int size){
        Page<Coupon> coupons = couponService.QueryAllPageCoupon(page, size);
        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "查询成功", coupons);
    }

    /*
     *查询当前可以使用的优惠券
     */
    /*
     * POSTMAN 测试成功
     */
    @ApiOperation(value = "查询当前可以使用的优惠券 <商城>")
    @PostMapping("queryQualifiedCoupon")
    public Result queryQualifiedCoupon(@RequestBody QualifiedCouponQueryDTO qualifiedCouponQueryDTO){
        List<Coupon> coupons = couponService.queryQualifiedCoupon(qualifiedCouponQueryDTO);
        List<CouponVO> couponVOS = new ArrayList<>();
        for(Coupon coupon:coupons) {
            CouponVO couponVO = new CouponVO();
            couponVO.setCouponName(coupon.getCouponName());
            couponVO.setThreshold(coupon.getThreshold());
            couponVO.setDiscount_price(coupon.getDiscountPrice());
            couponVO.setBeginTime(coupon.getBeginTime());
            couponVO.setEndTime(coupon.getEndTime());
            couponVOS.add(couponVO);
        }
        return Result.build(ResultCodeEnum.SUCCESS.getCode(), "查询成功", couponVOS);
    }

}
